<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="建设项目" prop="projectCode">
        <el-select
          v-model="queryParams.projectCode"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in projectOptions"
            :key="item.projectCode"
            :value="item.projectCode"
            :label="item.projectName"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="流程类型" prop="workflowType">
        <el-select v-model="queryParams.workflowType" placeholder="请选择流程类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.workflow_meterage_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workflow:meterage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['workflow:meterage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workflow:meterage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handlePaste"
          v-hasPermi="['workflow:meterage:add']"
        >粘贴</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="meterageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程UUID" align="center" prop="workflowUuid" width="350px" />
      <el-table-column label="建设项目" align="center" prop="projectName" />
      <el-table-column label="流程类型" align="center" prop="workflowType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.workflow_meterage_type" :value="scope.row.workflowType"/>
        </template>
      </el-table-column>
      <el-table-column label="流程名称" align="center" prop="workflowName" />
      <el-table-column label="流程状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['workflow:meterage:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['workflow:meterage:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-copy-document"
            @click="handleNode(scope.row)"
          >流程节点</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程管理-计量流程定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="建设项目" prop="projectCode">
          <el-select v-model="form.projectCode" placeholder="请选择建设项目" style="width: 100%">
            <el-option
              v-for="item in projectOptions"
              :key="item.projectCode"
              :label="item.projectName"
              :value="item.projectCode">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="流程类型" prop="workflowType">
          <el-select v-model="form.workflowType" placeholder="请选择流程类型" style="width: 100%">
            <el-option
              v-for="dict in dict.type.workflow_meterage_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程名称" prop="workflowName">
          <el-input v-model="form.workflowName" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item label="流程状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="流程备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入流程备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 粘贴计量流程定义对话框 -->
    <el-dialog :title="paste.title" :visible.sync="paste.open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="pasteForm" :model="paste.form" :rules="paste.rules" label-width="80px">

        <el-form-item label="流程名称" prop="workflowName">
          <el-input v-model="paste.form.workflowName" placeholder="请输入流程名称" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPasteForm">确 定</el-button>
        <el-button @click="cancelPaste">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMeterage, getMeterage, delMeterage, addMeterage, updateMeterage, pasteMeterage } from "@/api/workflow/workflowMeterage";
import { selectProjectList } from "@/api/project/project";

export default {
  name: "Meterage",
  dicts: ['workflow_meterage_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程管理-计量流程定义表格数据
      meterageList: [],
      // 建设项目选项
      projectOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCode: null,
        workflowType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectCode: [
          { required: true, message: "建设项目不能为空", trigger: "blur" },
        ],
        workflowType: [
          { required: true, message: "流程类型不能为空", trigger: "blur" },
        ],
        workflowName: [
          { required: true, message: "流程名称不能为空", trigger: "blur" },
          { max: 64, message: '流程名称最大长度为50个字符', trigger: 'blur'}
        ],
        remark: [
          { max: 255, message: '流程备注最大长度为50个字符', trigger: 'blur'}
        ],
      },
      // 粘贴参数
      paste: {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          workflowName: [
            { required: true, message: "流程名称不能为空", trigger: "blur" },
            { max: 64, message: '流程名称最大长度为50个字符', trigger: 'blur'}
          ]
        },
      }
    };
  },
  created() {
    this.getProjectList();
    this.getList();
  },
  methods: {
    /** 建设项目下拉 */
    getProjectList() {
      selectProjectList().then().then(response => {
        this.projectOptions = response.projects;
      })
    },
    /** 查询流程管理-计量流程定义列表 */
    getList() {
      this.loading = true;
      listMeterage(this.queryParams).then(response => {
        this.meterageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelPaste() {
      this.paste.open = false;
      this.resetPaste();
    },
    // 表单重置
    reset() {
      this.form = {
        workflowUuid: null,
        projectCode: null,
        projectName: null,
        workflowType: null,
        workflowName: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    // 粘贴表单重置
    resetPaste() {
      this.paste.form = {
        workflowUuid: null,
        workflowName: null
      };
      this.resetForm("pasteForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workflowUuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加计量流程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workflowUuid = row.workflowUuid || this.ids;
      getMeterage(workflowUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计量流程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 遍历数组
          let projectName;
          Object.keys(this.projectOptions).forEach((item) => {
            if (this.form.projectCode == this.projectOptions[item].projectCode) {
              projectName = this.projectOptions[item].projectName;
              this.form.projectName = projectName;
            }
          })
          if (this.form.workflowUuid != null) {
            updateMeterage(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMeterage(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workflowUuids = row.workflowUuid || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delMeterage(workflowUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 流程节点按钮操作 */
    handleNode(row) {
      const workflowUuid = row.workflowUuid;
      this.$router.push({ path: '/workflow/meterage/node/index', query: { workflowUuid: workflowUuid, projectCode: row.projectCode} });
    },
    /** 粘贴按钮操作 */
    handlePaste(row) {
      this.resetPaste();
      const workflowUuid = row.workflowUuid || this.ids;
      this.paste.form.workflowUuid = workflowUuid[0];
      this.paste.open = true;
      this.paste.title = "粘贴计量流程";
    },
    /** 粘贴提交按钮 */
    submitPasteForm() {
      this.$refs["pasteForm"].validate(valid => {
        if (valid) {
          console.log(this.paste.form);
          pasteMeterage(this.paste.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.paste.open = false;
            this.getList();
          });
        }
      });
    },
  }
};
</script>
